<?php
class DB{
    public $dbHost  = "localhost";
    public $dbUser  = "root";
    public $dbPwd   = "12345678";
    public $dbName  = "yp_wenzhoumj";
    public $links;
    function __construct(){ //链接数据库
        $this->links = mysql_connect($this->dbHost,$this->dbUser,$this->dbPwd);
        mysql_query("set names utf8");
        mysql_select_db("yp_wenzhoumj",$this->links);
    }
    function query($sql){  // 增 删 改      查询
       return mysql_query($sql); 
    }
    //数据库操作： 得到1条记录 select ...where id=2       得到多条记录 select * form ...    
    function fetch($sql){
        $result = $this->query($sql); //$this 可以调用当前类的属性 和 方法
        $rs = mysql_fetch_assoc($result);
        return $rs; //一维 数组
    }
    function fetchAll($sql){ //多条记录  返回 二维数组
        $result = $this->query($sql); 
        $arr = array();
        while($rs = mysql_fetch_assoc($result)){
           $arr[] = $rs; // $rs  $rs $rs 每个都是一维   $arr[0] = $rs $arr[1] = $rs 
        }
        return $arr;
    }   
	function numRows($sql){ //统计sql 的记录数
		$result = $this->query($sql);
		$count = mysql_num_rows($result);
		return $count;
	}
    function __destruct(){
        $this->dbHost = null;
        $this->dbUser = null;
        $this->dbPwd = null;
        $this->dbName = null;
        $this->links = null;
    }    
}